package pl.edu.agh.ki.ewywiadowka.client.windows.secretary;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.Const.FormularMessages;
import pl.edu.agh.ki.ewywiadowka.client.Const.Error.TeacherRegisterMsg;
import pl.edu.agh.ki.ewywiadowka.client.dto.SubjectDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.TeacherDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;

public class RegisterTeacherWindow extends GenericWindow implements
		ClickHandler {

	private VerticalPanel _verticalPanel = new VerticalPanel();

	private ScrollPanel _mainPanel = new ScrollPanel();

	private Grid _formular = null;

	private String[] _fields = { "Imię", "Nazwisko", "Adres", "Telefon", "Login", "Haslo", "Przedmiot"};

	public TextBox _nameTextBox = new TextBox();
	public TextBox _surnameTextBox = new TextBox();
	public TextBox _addressTextBox = new TextBox();
	public TextBox _telephoneTextBox = new TextBox();
	public TextBox _emailTextBox = new TextBox();
	public TextBox _loginTextBox = new TextBox();
	public TextBox _passTextBox = new TextBox();
	public ListBox _subjectTextBox = new ListBox(true);
	
	public LinkedList<SubjectDTO> _subjects = null;

	public Button _okButton = new Button("Dodaj nauczyciela");

	public RegisterTeacherWindow(EWywiadowka wywiadowka) {
		super(wywiadowka);

		_formular = new Grid(_fields.length, 2); // trzecia kolumna dla
													// podpowiedzi, weryfikacji
		for (int i = 0; i < _fields.length; i++)
			_formular.setWidget(i, 0, new Label(_fields[i]));

		int j = 0;
		_formular.setWidget(j++, 1, _surnameTextBox);
		_formular.setWidget(j++, 1, _nameTextBox);
		_formular.setWidget(j++, 1, _addressTextBox);
		_formular.setWidget(j++, 1, _telephoneTextBox);
		_formular.setWidget(j++, 1, _loginTextBox);
		_formular.setWidget(j++, 1, _passTextBox);
		_subjectTextBox.setVisibleItemCount(5);
		_formular.setWidget(j++, 1, _subjectTextBox);

		_formular.setBorderWidth(2);

		_okButton.addClickHandler(this);

		_verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_verticalPanel.add(_formular);
		_verticalPanel.add(_okButton);

		_mainPanel.add(_verticalPanel);

		this.initWidget(_mainPanel);
		Const.eWywiadowkaService.getSubjects(new AsyncCallback<LinkedList<SubjectDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(LinkedList<SubjectDTO> result) {
				_subjects = result;
				
				_subjectTextBox.clear();
				
				for (SubjectDTO subject : _subjects)
					_subjectTextBox.addItem(subject.toString());
				
			}
			
		});
	}

	public Const.FormularMessages verifyFormular() {
		if (_surnameTextBox.getText().length() == 0)
			return FormularMessages.WRONG_SURNAME;
		if (_nameTextBox.getText().length() == 0)
			return FormularMessages.WRONG_NAME;
		if (_addressTextBox.getText().length() == 0)
			return FormularMessages.WRONG_ADDRESS;
		if (_telephoneTextBox.getText().length() == 0)
			return FormularMessages.WRONG_ATTENDANT_PHONE;
		if (_loginTextBox.getText().length() == 0)
			return FormularMessages.WRONG_LOGIN;
		if (_passTextBox.getText().length() == 0)
			return FormularMessages.WRONG_PASS;
		if (_subjectTextBox.getSelectedIndex() == -1)
			return FormularMessages.WRONG_SUBJECT;
		return Const.FormularMessages.OK;
	}

	private void teacherRegister() {
		
		LinkedList<SubjectDTO> subjects = new LinkedList<SubjectDTO>();
		
		for (int i = 0; i < _subjects.size(); i++)
			if (_subjectTextBox.isItemSelected(i))
				subjects.add(_subjects.get(i));
		
		if (subjects.size() == 0) {
			Window.alert("Prosze wybrac przynajmniej jeden nauczany przedmiot");
			return;
		}
		
		TeacherDTO teacher = new TeacherDTO(null,_surnameTextBox.getText(), _nameTextBox.getText(), _addressTextBox.getText(), _telephoneTextBox.getText(), null, subjects);
		
		Const.eWywiadowkaService.registerTeacher(teacher, new AsyncCallback<TeacherRegisterMsg>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(TeacherRegisterMsg result) {
				Window.alert(result.toString());
			}
			
		});
	}

	public void onClick(ClickEvent event) {
		Const.FormularMessages msg = null;
		if ((msg = verifyFormular()) == Const.FormularMessages.OK)
			teacherRegister();
		else
			Window.alert(msg.toString());
	}

}
